package com.ruffian.library;


import com.ruffian.library.utils.AttrUtils;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentState;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.utils.TextTool;
import ohos.app.Context;
import ohos.app.Environment;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.multimodalinput.event.TouchEvent;

import java.io.*;


/**
 * RTextView
 * <p>
 * 1. RTextView 让你从此不再编写和管理大量 selector 文件（这个太可恨了）
 * 2. RTextView 改造了 drawableLeft/drawableXXX 图片的大小，
 * 从此你不在需要使用 LinearLayout + ImageView + TextView 就能够直接实现文字带图片的功能，关键还能设置icon大小
 * 3. RTextView 能够直接设置各种圆角效果： 四周圆角，某一方向圆角，甚至椭圆，圆形都简单实现。 边框效果，虚线边框都是一个属性搞定
 * 4. RTextView 不仅能够定义默认状态的背景，边框，连按下/点击状态通通一起搞定
 * 5. RTextView 按下变色支持：背景色，边框，文字，drawableLeft/xxx （这个赞啊）
 *
 * @author ZhongDaFeng
 */
public class RTextView extends Text implements Component.TouchEventListener,
        Component.ComponentStateChangedListener {
    /**
     * 左侧图标.
     */
    public static final String ICON_DIR_LEFT = "left";
    /**
     * 顶部图标.
     */
    public static final String ICON_DIR_TOP = "top";
    /**
     * 右侧图标.
     */
    public static final String ICON_DIR_RIGHT = "right";
    /**
     * 底部图标.
     */
    public static final String ICON_DIR_BOTTOM = "bottom";

    //icon
    private int mIconHeight;
    private int mIconWidth;
    private String mIconDirection;

    //corner
    private float mCornerRadius;
    private float mCornerRadiusTopLeft;
    private float mCornerRadiusTopRight;
    private float mCornerRadiusBottomLeft;
    private float mCornerRadiusBottomRight;

    //BorderWidth
    private float mBorderDashWidth = 0;
    private float mBorderDashGap = 0;
    private int mBorderWidthNormal = 0;
    private int mBorderWidthPressed = 0;
    private int mBorderWidthUnable = 0;

    //BorderColor
    private int mBorderColorNormal = 0;
    private int mBorderColorPressed = 0;
    private int mBorderColorUnable = 0;

    //Background
    private int mBackgroundColorNormal;
    private int mBackgroundColorPressed;
    private int mBackgroundColorUnable;
    private ShapeElement mBackgroundNormal;
    private ShapeElement mBackgroundPressed;
    private ShapeElement mBackgroundUnable;


    // Text
    private int mTextColorNormal;
    private int mTextColorPressed;
    private int mTextColorUnable;


    //Icon
    private Element mIcon = null;
    private Element mIconNormal;
    private Element mIconPressed;
    private Element mIconUnable;

    //typeface
    private String mTypefacePath;

    private final int[][] states = new int[4][];
    private StateElement mStateBackground;
    private final float[] mBorderRadii = new float[8];


    private final Context mContext;


    private boolean mHasPressedBgColor = false;
    private boolean mHasUnableBgColor = false;
    private boolean mHasPressedBorderColor = false;
    private boolean mHasUnableBorderColor = false;
    private boolean mHasPressedBorderWidth = false;
    private boolean mHasUnableBorderWidth = false;

    /**
     * @param context
     */
    public RTextView(final Context context) throws IOException {
        this(context, null);
    }

    /**
     * @param context
     * @param attrs
     */
    public RTextView(final Context context, final AttrSet attrs) throws IOException {
        super(context, attrs);
        mContext = context;
        initAttributeSet(context, attrs);
        setComponentStateChangedListener(this);
        setTouchEventListener(this);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if (enabled) {
            if (mIconNormal != null) {
                mIcon = mIconNormal;
                setIcon();
            }
        } else {
            if (mIconUnable != null) {
                mIcon = mIconUnable;
                setIcon();
            }
        }
    }


    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        if (!isEnabled()) {
            return true;
        }
        int action = touchEvent.getAction();
        switch (action) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                setPressState(true);
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                setPressState(false);
                break;
            case TouchEvent.POINT_MOVE:
                int index = touchEvent.getIndex();
                int x = (int) touchEvent.getPointerScreenPosition(index).getX();
                int y = (int) touchEvent.getPointerScreenPosition(index).getY();
                if (isOutsideView(x, y)) {
                    setPressState(false);
                }
                break;
            case TouchEvent.CANCEL:
                setPressState(false);
                break;
            default:
                break;
        }

        return true;
    }


    /**
     * 初始化控件属性
     *
     * @param context 上下文
     * @param attrs   属性
     */
    private void initAttributeSet(Context context, AttrSet attrs) throws IOException {
        if (context == null || attrs == null) {
            setup();
            return;
        }
        mCornerRadius = AttrUtils.getDimensionFromAttr(attrs, "corner_radius", 0);
        mCornerRadiusTopLeft = AttrUtils.getDimensionFromAttr(attrs, "corner_radius_top_left", 0);
        mCornerRadiusTopRight = AttrUtils.getDimensionFromAttr(attrs, "corner_radius_top_right", 0);
        mCornerRadiusBottomLeft = AttrUtils.getDimensionFromAttr(attrs, "corner_radius_bottom_left", 0);
        mCornerRadiusBottomRight = AttrUtils.getDimensionFromAttr(attrs, "corner_radius_bottom_right", 0);
        //border
        mBorderDashWidth = AttrUtils.getDimensionFromAttr(attrs, "border_dash_width", 0);
        mBorderDashGap = AttrUtils.getDimensionFromAttr(attrs, "border_dash_gap", 0);
        mBorderWidthNormal = AttrUtils.getDimensionFromAttr(attrs, "border_width_normal", 0);
        mBorderWidthPressed = AttrUtils.getDimensionFromAttr(attrs, "border_width_pressed", 0);
        mBorderWidthUnable = AttrUtils.getDimensionFromAttr(attrs, "border_width_unable", 0);
        mBorderColorNormal = AttrUtils.getColorFromAttr(attrs, "border_color_normal", 0);
        mBorderColorPressed = AttrUtils.getColorFromAttr(attrs, "border_color_pressed", 0);
        mBorderColorUnable = AttrUtils.getColorFromAttr(attrs, "border_color_unable", 0);
        //icon
        //Vector兼容处理
        mIconNormal = AttrUtils.getElementFromAttr(attrs, "icon_src_normal", null);
        mIconPressed = AttrUtils.getElementFromAttr(attrs, "icon_src_pressed", null);
        mIconUnable = AttrUtils.getElementFromAttr(attrs, "icon_src_unable", null);
        mIconWidth = AttrUtils.getDimensionFromAttr(attrs, "icon_width", 0);
        mIconHeight = AttrUtils.getDimensionFromAttr(attrs, "icon_height", 0);
        mIconDirection = AttrUtils.getStringFromAttr(attrs, "icon_direction", ICON_DIR_LEFT);
        //text
        mTextColorNormal = AttrUtils.getColorFromAttr(attrs, "text_color_normal", getTextColor().getValue());
        mTextColorPressed = AttrUtils.getColorFromAttr(attrs, "text_color_pressed", getTextColor().getValue());
        mTextColorUnable = AttrUtils.getColorFromAttr(attrs, "text_color_unable", getTextColor().getValue());
        //background
        mBackgroundColorNormal = AttrUtils.getColorFromAttr(attrs, "background_normal", 0);

        mBackgroundColorPressed = AttrUtils.getColorFromAttr(attrs, "background_pressed", 0);
        mBackgroundColorUnable = AttrUtils.getColorFromAttr(attrs, "background_unable", 0);
        //typeface
        mTypefacePath = AttrUtils.getStringFromAttr(attrs, "text_typeface", "");


        mHasPressedBgColor = mBackgroundColorPressed != 0;
        mHasUnableBgColor = mBackgroundColorUnable != 0;
        mHasPressedBorderColor = mBorderColorPressed != 0;
        mHasUnableBorderColor = mBorderColorUnable != 0;
        mHasPressedBorderWidth = mBorderWidthPressed != 0;
        mHasUnableBorderWidth = mBorderWidthUnable != 0;
        setup();

    }


    private void setup() throws IOException {
        mBackgroundNormal = new ShapeElement();
        mBackgroundPressed = new ShapeElement();
        mBackgroundUnable = new ShapeElement();

        Element drawable = getBackgroundElement();
        if (drawable instanceof StateElement) {
            mStateBackground = (StateElement) drawable;
        } else {
            mStateBackground = new StateElement();
        }

        if (!mHasPressedBgColor) {
            mBackgroundColorPressed = mBackgroundColorNormal;
        }
        if (!mHasUnableBgColor) {
            mBackgroundColorUnable = mBackgroundColorNormal;
        }

        mBackgroundNormal.setRgbColor(RgbColor.fromArgbInt(mBackgroundColorNormal));
        mBackgroundPressed.setRgbColor(RgbColor.fromArgbInt(mBackgroundColorPressed));
        mBackgroundUnable.setRgbColor(RgbColor.fromArgbInt(mBackgroundColorUnable));

        //pressed, focused, normal, unable

        states[0] = new int[]{ComponentState.COMPONENT_STATE_PRESSED};
        states[1] = new int[]{ComponentState.COMPONENT_STATE_FOCUSED};
        states[3] = new int[]{ComponentState.COMPONENT_STATE_EMPTY};
        states[2] = new int[]{ComponentState.COMPONENT_STATE_DISABLED};
        mStateBackground.addState(states[0], mBackgroundPressed);
        mStateBackground.addState(states[1], mBackgroundPressed);
        mStateBackground.addState(states[3], mBackgroundNormal);
        mStateBackground.addState(states[2], mBackgroundUnable);

        //Icon
        if (!isEnabled()) {
            mIcon = mIconUnable;
        } else {
            mIcon = mIconNormal;
        }

        //设置边框默认值
        setCommonBorderValue();

        setBackgroundState(mBackgroundColorNormal == 0 && mBackgroundColorUnable == 0 && mBackgroundColorPressed == 0);

        //设置文本颜色
        setTextColor();

        //设置边框
        setBorder();

        //设置ICON
        setIcon();

        //设置圆角
        setRadius();

        //设置文本字体样式
        setTypeface();


    }

    private void setCommonBorderValue() {
        if (!mHasPressedBorderWidth) {
            mBorderWidthPressed = mBorderWidthNormal;
        }
        if (!mHasUnableBorderWidth) {
            mBorderWidthUnable = mBorderWidthNormal;
        }
        if (!mHasPressedBorderColor) {
            mBorderColorPressed = mBorderColorNormal;
        }
        if (!mHasUnableBorderColor) {
            mBorderColorUnable = mBorderColorNormal;
        }
    }

    /**
     * touch是否移出view之外
     *
     * @param x touchEvent.getPointerScreenPosition.getX()
     * @param y touchEvent.getPointerScreenPosition.getY()
     * @return boolean 是否超出View范围之外
     */
    private boolean isOutsideView(int x, int y) {
        int[] location = getLocationOnScreen();
        int pointx = x - location[0];
        int pointy = y - location[1];
        boolean flag = false;
        Rect rect = new Rect();
        getSelfVisibleRect(rect);
        if (!rect.isInclude(pointx, pointy)) {
            flag = true;
        }
        return flag;
    }

    /**
     * 设置背景颜色
     *
     * @param normal
     * @param pressed
     * @param unable
     * @return RTextView
     */
    public RTextView setStateBackgroundColor(int normal, int pressed, int unable) {
        mBackgroundColorNormal = normal;
        mBackgroundColorPressed = pressed;
        mBackgroundColorUnable = unable;
        mHasPressedBgColor = true;
        mHasUnableBgColor = true;
        mBackgroundNormal.setRgbColor(RgbColor.fromArgbInt(mBackgroundColorNormal));
        mBackgroundPressed.setRgbColor(RgbColor.fromArgbInt(mBackgroundColorPressed));
        mBackgroundUnable.setRgbColor(RgbColor.fromArgbInt(mBackgroundColorUnable));
        setBackgroundState(false);
        return this;
    }

    /**
     * 获取{@link ComponentState#COMPONENT_STATE_EMPTY}状态下的背景颜色
     *
     * @return mBackgroundColorNormal
     */
    public int getBackgroundColorNormal() {
        return mBackgroundColorNormal;
    }

    /**
     * 设置{@link ComponentState#COMPONENT_STATE_EMPTY}状态下的背景颜色
     *
     * @param colorNormal
     * @return RTextView
     */
    public RTextView setBackgroundColorNormal(int colorNormal) {
        this.mBackgroundColorNormal = colorNormal;
        //设置背景默认值
        if (!mHasPressedBgColor) {
            mBackgroundColorPressed = mBackgroundColorNormal;
            mBackgroundPressed.setRgbColor(RgbColor.fromArgbInt(mBackgroundColorPressed));
        }
        if (!mHasUnableBgColor) {
            mBackgroundColorUnable = mBackgroundColorNormal;
            mBackgroundUnable.setRgbColor(RgbColor.fromArgbInt(mBackgroundColorUnable));
        }
        mBackgroundNormal.setRgbColor(RgbColor.fromArgbInt(mBackgroundColorNormal));
        setBackgroundState(false);
        return this;
    }

    /**
     * 获取{@link ComponentState#COMPONENT_STATE_PRESSED}状态下的背景颜色
     *
     * @return mBackgroundColorPressed
     */
    public int getBackgroundColorPressed() {
        return mBackgroundColorPressed;
    }

    /**
     * 设置{@link ComponentState#COMPONENT_STATE_PRESSED}状态下的背景颜色
     *
     * @param colorPressed
     * @return RTextView
     */
    public RTextView setBackgroundColorPressed(int colorPressed) {
        this.mBackgroundColorPressed = colorPressed;
        this.mHasPressedBgColor = true;
        mBackgroundPressed.setRgbColor(RgbColor.fromArgbInt(mBackgroundColorPressed));
        setBackgroundState(false);
        return this;
    }

    /**
     * 获取{@link ComponentState#COMPONENT_STATE_DISABLED}状态下的背景颜色
     *
     * @return mBackgroundColorUnable
     */
    public int getBackgroundColorUnable() {
        return mBackgroundColorUnable;
    }

    /**
     * 设置{@link ComponentState#COMPONENT_STATE_DISABLED}状态下的背景颜色
     *
     * @param colorUnable
     * @return RTextView
     */
    public RTextView setBackgroundColorUnable(int colorUnable) {
        this.mBackgroundColorUnable = colorUnable;
        this.mHasUnableBgColor = true;
        mBackgroundUnable.setRgbColor(RgbColor.fromArgbInt(mBackgroundColorUnable));
        setBackgroundState(false);
        return this;
    }


    private void setBackgroundState(boolean unset) {

        Element element = getBackgroundElement();
        setBackground(unset ? element : mStateBackground);
    }


    /**
     * @param typefacePath
     * @return RTextView
     */
    public RTextView setTypeface(String typefacePath) throws IOException {
        this.mTypefacePath = typefacePath;
        setTypeface();
        return this;
    }

    /**
     * @return String 第三方字体名称
     */
    public String getTypefacePath() {
        return mTypefacePath;
    }


    private void setTypeface() throws IOException {
        if (TextTool.isNullOrEmpty(mTypefacePath)) {
            return;
        }
        StringBuffer fileName = new StringBuffer(mTypefacePath);
        File file = new File(mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES), fileName.toString());
        OutputStream outputStream = null;
        ResourceManager resManager = getContext().getResourceManager();
        RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/fonts/" + mTypefacePath);
        Resource resource = null;
        try {
            resource = rawFileEntry.openRawFile();
            outputStream = new FileOutputStream(file);
            int index;
            byte[] bytes = new byte[1024];
            if (resource != null) {
                while ((index = resource.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, index);
                    outputStream.flush();
                }
                Font.Builder builder = new Font.Builder(file);
                Font font = builder.build();
                setFont(font);
            }
        } catch (FileNotFoundException e) {
            //e.printStackTrace();
        } catch (NullPointerException e) {
            //e.printStackTrace();
        } finally {
            resource.close();
            outputStream.close();
        }

    }

    /**
     * @param icon
     * @return RTextView
     */

    public RTextView setIconNormal(Element icon) {
        this.mIconNormal = icon;
        this.mIcon = icon;
        setIcon();
        return this;
    }

    /**
     * @return mIconNormal
     */
    public Element getIconNormal() {
        return mIconNormal;
    }

    /**
     * @param icon
     * @return RTextView
     */
    public RTextView setIconPressed(Element icon) {
        this.mIconPressed = icon;
        this.mIcon = icon;
        setIcon();
        return this;
    }

    /**
     * @return Element
     */
    public Element getIconPressed() {
        return mIconPressed;
    }

    /**
     * @param icon
     * @return RTextView
     */
    public RTextView setIconUnable(Element icon) {
        this.mIconUnable = icon;
        this.mIcon = icon;
        setIcon();
        return this;
    }

    /**
     * @return Element
     */
    public Element getIconUnable() {
        return mIconUnable;
    }

    /**
     * @param iconWidth
     * @param iconHeight
     * @return RTextView
     */
    public RTextView setIconSize(int iconWidth, int iconHeight) {
        this.mIconWidth = iconWidth;
        this.mIconHeight = iconHeight;
        setIcon();
        return this;
    }

    /**
     * @param iconWidth
     * @return RTextView
     */
    public RTextView setIconWidth(int iconWidth) {
        this.mIconWidth = iconWidth;
        setIcon();
        return this;
    }

    /**
     * @return RTextView
     */
    public int getIconWidth() {
        return mIconWidth;
    }

    /**
     * @param iconHeight
     * @return RTextView
     */
    public RTextView setIconHeight(int iconHeight) {
        this.mIconHeight = iconHeight;
        setIcon();
        return this;
    }

    /**
     * @return int
     */
    public int getIconHeight() {
        return mIconHeight;
    }

    /**
     * @param iconDirection
     * @return RTextView
     */
    public RTextView setIconDirection(String iconDirection) {
        this.mIconDirection = iconDirection;
        setIcon();
        return this;
    }

    /**
     * @return String
     */
    public String getIconDirection() {
        return mIconDirection;
    }

    private void setIcon() {
        //未设置图片大小
        if (isPressed()) {
            mIcon = mIconPressed;
        } else {
            if (isEnabled()) {
                mIcon = mIconNormal;
            } else {
                mIcon = mIconUnable;
            }
        }
        if (mIconHeight == 0 && mIconWidth == 0) {
            if (mIcon != null) {
                mIconWidth = mIcon.getWidth();
                mIconHeight = mIcon.getHeight();
            }
        }
        setIcon(mIcon, mIconWidth, mIconHeight, mIconDirection);
    }

    private void setIcon(Element element, int width, int height, String direction) {
        if (element != null) {
            if (width != 0 && height != 0) {
                element.setBounds(0, 0, width, height);
            }
            switch (direction) {
                case ICON_DIR_LEFT:
                    setAroundElements(element, null, null, null);

                    break;
                case ICON_DIR_TOP:
                    setAroundElements(null, element, null, null);
                    break;
                case ICON_DIR_RIGHT:
                    setAroundElements(null, null, element, null);
                    break;
                case ICON_DIR_BOTTOM:
                    setAroundElements(null, null, null, element);
                    break;
            }
        }
    }

    /**
     * @param textColor
     * @return RTextView
     */

    public RTextView setTextColorNormal(int textColor) {
        this.mTextColorNormal = textColor;
        if (mTextColorPressed == 0) {
            mTextColorPressed = mTextColorNormal;
        }
        if (mTextColorUnable == 0) {
            mTextColorUnable = mTextColorNormal;
        }
        setTextColor();
        return this;
    }

    /**
     * @return int
     */
    public int getTextColorNormal() {
        return mTextColorNormal;
    }

    /**
     * @param textColor
     * @return RTextView
     */
    public RTextView setPressedTextColor(int textColor) {
        this.mTextColorPressed = textColor;
        setTextColor();
        return this;
    }

    /**
     * @return int
     */
    public int getPressedTextColor() {
        return mTextColorPressed;
    }

    /**
     * @param textColor
     * @return RTextView
     */
    public RTextView setTextColorUnable(int textColor) {
        this.mTextColorUnable = textColor;
        setTextColor();
        return this;
    }

    /**
     * @return int
     */
    public int getTextColorUnable() {
        return mTextColorUnable;
    }

    /**
     * @param normal
     * @param pressed
     * @param unable
     */
    public void setTextColor(int normal, int pressed, int unable) {
        this.mTextColorNormal = normal;
        this.mTextColorPressed = pressed;
        this.mTextColorUnable = unable;
        setTextColor();
    }

    private void setTextColor() {
        if (isPressed()) {
            setTextColor(new Color(mTextColorPressed));
        } else {
            if (isEnabled()) {
                setTextColor(new Color(mTextColorNormal));
            } else {
                setTextColor(new Color(mTextColorUnable));
            }

        }
    }

    /**
     * @param width
     * @return RTextView
     */
    public RTextView setBorderWidthNormal(int width) {
        this.mBorderWidthNormal = width;
        if (!mHasPressedBorderWidth) {
            mBorderWidthPressed = mBorderWidthNormal;
            setBorderPressed();
        }
        if (!mHasUnableBorderWidth) {
            mBorderWidthUnable = mBorderWidthNormal;
            setBorderUnable();
        }
        setBorderNormal();
        return this;
    }

    /**
     * @return int mBorderWidthNormal
     */
    public int getBorderWidthNormal() {
        return mBorderWidthNormal;
    }

    /**
     * @param color
     * @return RTextView
     */
    public RTextView setBorderColorNormal(int color) {
        this.mBorderColorNormal = color;
        if (!mHasPressedBorderColor) {
            mBorderColorPressed = mBorderColorNormal;
            setBorderPressed();
        }
        if (!mHasUnableBorderColor) {
            mBorderColorUnable = mBorderColorNormal;
            setBorderUnable();
        }
        setBorderNormal();
        return this;
    }

    /**
     * @return mBorderColorNormal
     */
    public int getBorderColorNormal() {
        return mBorderColorNormal;
    }

    /**
     * @param width
     * @return RTextView
     */
    public RTextView setBorderWidthPressed(int width) {
        this.mBorderWidthPressed = width;
        this.mHasPressedBorderWidth = true;
        setBorderPressed();
        return this;
    }

    /**
     * @return mBorderWidthPressed
     */
    public int getBorderWidthPressed() {
        return mBorderWidthPressed;
    }

    /**
     * @param color
     * @return RTextView
     */
    public RTextView setBorderColorPressed(int color) {
        this.mBorderColorPressed = color;
        this.mHasPressedBorderColor = true;
        setBorderPressed();
        return this;
    }

    /**
     * @return mBorderColorPressed
     */
    public int getBorderColorPressed() {
        return mBorderColorPressed;
    }

    /**
     * @param width
     * @return RTextView
     */
    public RTextView setBorderWidthUnable(int width) {
        this.mBorderWidthUnable = width;
        this.mHasUnableBorderWidth = true;
        setBorderUnable();
        return this;
    }

    /**
     * @return mBorderWidthUnable
     */
    public int getBorderWidthUnable() {
        return mBorderWidthUnable;
    }

    /**
     * 设置ComponentState.COMPONENT_STATE_DISABLED 下的border颜色
     *
     * @param color
     * @return RTextView
     */
    public RTextView setBorderColorUnable(int color) {
        this.mBorderColorUnable = color;
        this.mHasUnableBorderColor = true;
        setBorderUnable();
        return this;
    }

    /**
     * 获取ComponentState.COMPONENT_STATE_DISABLED 下的border颜色
     *
     * @return int
     */
    public int getBorderColorUnable() {
        return mBorderColorUnable;
    }

    /**
     * 设置border 宽度
     *
     * @param normal  {ComponentState.COMPONENT_STATE_EMPTY} 状态下的宽度
     * @param pressed {ComponentState.COMPONENT_STATE_PRESSED} 状态下的宽度
     * @param unable  {ComponentState.COMPONENT_STATE_DISABLED} 状态下的宽度
     */
    public void setBorderWidth(int normal, int pressed, int unable) {
        this.mBorderWidthNormal = normal;
        this.mBorderWidthPressed = pressed;
        this.mBorderWidthUnable = unable;
        this.mHasPressedBorderWidth = true;
        this.mHasUnableBorderWidth = true;
        setBorder();
    }

    /**
     * 设置border 颜色
     *
     * @param normal  {ComponentState.COMPONENT_STATE_EMPTY} 状态下的颜色
     * @param pressed {ComponentState.COMPONENT_STATE_PRESSED} 状态下的颜色
     * @param unable  {ComponentState.COMPONENT_STATE_DISABLED} 状态下的颜色
     */
    public void setBorderColor(int normal, int pressed, int unable) {
        this.mBorderColorNormal = normal;
        this.mBorderColorPressed = pressed;
        this.mBorderColorUnable = unable;
        this.mHasPressedBorderColor = true;
        this.mHasUnableBorderColor = true;
        setBorder();
    }

    /**
     * 设置dashWidth
     *
     * @param dashWidth
     */
    public void setBorderDashWidth(float dashWidth) {
        this.mBorderDashWidth = dashWidth;
        setBorder();
    }

    /**
     * 获取DashWidth
     *
     * @return mBorderDashWidth
     */
    public float getBorderDashWidth() {
        return mBorderDashWidth;
    }

    /**
     * 设置dashGap
     *
     * @param dashGap
     */
    public void setBorderDashGap(float dashGap) {
        this.mBorderDashGap = dashGap;
        setBorder();
    }

    /**
     * 获取 BorderDashGap
     *
     * @return mBorderDashGap
     */
    public float getBorderDashGap() {
        return mBorderDashGap;
    }

    /**
     * 配置虚线
     *
     * @param dashWidth dashWidth
     * @param dashGap   dashWidth
     */
    public void setBorderDash(float dashWidth, float dashGap) {
        this.mBorderDashWidth = dashWidth;
        this.mBorderDashGap = dashGap;
        setBorder();
    }

    private void setBorder() {

        mBackgroundNormal.setStroke(mBorderWidthNormal, RgbColor.fromArgbInt(mBorderColorNormal));
        mBackgroundPressed.setStroke(mBorderWidthPressed, RgbColor.fromArgbInt(mBorderColorPressed));
        mBackgroundUnable.setStroke(mBorderWidthUnable, RgbColor.fromArgbInt(mBorderColorUnable));

        if (mBorderDashGap > 0 && mBorderDashWidth > 0) {
            float[] intervals = new float[]{mBorderDashWidth, mBorderDashGap};
            mBackgroundNormal.setDashPathEffectValues(intervals, 0);
            mBackgroundPressed.setDashPathEffectValues(intervals, 0);
            mBackgroundUnable.setDashPathEffectValues(intervals, 0);
        }
        setBackgroundState(false);
    }

    private void setBorderNormal() {
        mBackgroundNormal.setStroke(mBorderWidthNormal, RgbColor.fromArgbInt(mBorderColorNormal));
        setBackgroundState(false);
    }

    private void setBorderPressed() {
        mBackgroundPressed.setStroke(mBorderWidthPressed, RgbColor.fromArgbInt(mBorderColorPressed));
        setBackgroundState(false);
    }

    private void setBorderUnable() {
        mBackgroundUnable.setStroke(mBorderWidthUnable, RgbColor.fromArgbInt(mBorderColorUnable));
        setBackgroundState(false);
    }

    /**
     * 设置圆角半径
     *
     * @param radius
     */
    public void setCornerRadius(float radius) {
        this.mCornerRadius = radius;
        setRadius();
    }

    /**
     * 获取圆角半径
     *
     * @return mCornerRadius
     */
    public float getCornerRadius() {
        return mCornerRadius;
    }

    /**
     * 设置右上角的圆角半径
     *
     * @param topLeft
     * @return RTextView实例
     */
    public RTextView setCornerRadiusTopLeft(float topLeft) {
        this.mCornerRadius = -1;
        this.mCornerRadiusTopLeft = topLeft;
        setRadius();
        return this;
    }

    /**
     * @return mCornerRadiusTopLeft
     */
    public float getCornerRadiusTopLeft() {
        return mCornerRadiusTopLeft;
    }

    /**
     * 设置右下角的圆角半径
     *
     * @param topRight
     * @return RTextView实例
     */
    public RTextView setCornerRadiusTopRight(float topRight) {
        this.mCornerRadius = -1;
        this.mCornerRadiusTopRight = topRight;
        setRadius();
        return this;
    }

    /**
     * 获取右上角圆角半径
     *
     * @return mCornerRadiusTopRight
     */
    public float getCornerRadiusTopRight() {
        return mCornerRadiusTopRight;
    }

    /**
     * 设置右下角的圆角半径
     *
     * @param bottomRight
     * @return RTextView实例
     */
    public RTextView setCornerRadiusBottomRight(float bottomRight) {
        this.mCornerRadius = -1;
        this.mCornerRadiusBottomRight = bottomRight;
        setRadius();
        return this;
    }

    /**
     * 获取右下角圆角半径
     *
     * @return mCornerRadiusBottomRight
     */
    public float getCornerRadiusBottomRight() {
        return mCornerRadiusBottomRight;
    }

    /**
     * 设置左下角的圆角半径
     *
     * @param bottomLeft
     * @return RTextView实例
     */
    public RTextView setCornerRadiusBottomLeft(float bottomLeft) {
        this.mCornerRadius = -1;
        this.mCornerRadiusBottomLeft = bottomLeft;
        setRadius();
        return this;
    }

    /**
     * 获取右下角圆角半径
     *
     * @return mCornerRadiusBottomRight
     */
    public float getCornerRadiusBottomLeft() {
        return mCornerRadiusBottomLeft;
    }

    /**
     * 设置圆角
     *
     * @param topLeft     左上角
     * @param topRight    右上角
     * @param bottomRight 右下角
     * @param bottomLeft  左下角
     */
    public void setCornerRadius(float topLeft, float topRight, float bottomRight, float bottomLeft) {
        this.mCornerRadius = -1;
        this.mCornerRadiusTopLeft = topLeft;
        this.mCornerRadiusTopRight = topRight;
        this.mCornerRadiusBottomRight = bottomRight;
        this.mCornerRadiusBottomLeft = bottomLeft;
        setRadius();
    }

    private void setRadiusRadii() {
        mBackgroundNormal.setCornerRadiiArray(mBorderRadii);
        mBackgroundPressed.setCornerRadiiArray(mBorderRadii);
        mBackgroundUnable.setCornerRadiiArray(mBorderRadii);
        setBackgroundState(false);
    }

    private void setRadius() {
        if (mCornerRadius > 0) {
            mBorderRadii[0] = mCornerRadius;
            mBorderRadii[1] = mCornerRadius;
            mBorderRadii[2] = mCornerRadius;
            mBorderRadii[3] = mCornerRadius;
            mBorderRadii[4] = mCornerRadius;
            mBorderRadii[5] = mCornerRadius;
            mBorderRadii[6] = mCornerRadius;
            mBorderRadii[7] = mCornerRadius;
        } else if (mCornerRadius <= 0) {
            mBorderRadii[0] = mCornerRadiusTopLeft;
            mBorderRadii[1] = mCornerRadiusTopLeft;
            mBorderRadii[2] = mCornerRadiusTopRight;
            mBorderRadii[3] = mCornerRadiusTopRight;
            mBorderRadii[4] = mCornerRadiusBottomRight;
            mBorderRadii[5] = mCornerRadiusBottomRight;
            mBorderRadii[6] = mCornerRadiusBottomLeft;
            mBorderRadii[7] = mCornerRadiusBottomLeft;
        }
        setRadiusRadii();
    }

    @Override
    public void onComponentStateChanged(Component component, int i) {

        if (ComponentState.isStateMatched(i, ComponentState.COMPONENT_STATE_PRESSED)) {

            setTextColor();
            setBackgroundState(false);
            setBorder();
            setIcon();
        }
        if (i == 2048) {
            setTextColor();
            setBackgroundState(false);
            setBorder();
            setIcon();
        }

    }


}
